package com.onlineMIS.sorter;

import java.util.Comparator;

import org.apache.commons.lang.StringUtils;

import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrderProduct;

public class HeadQInventoryOrderProduct4Peihuo implements Comparator<InventoryOrderProduct> {

	@Override
	public int compare(InventoryOrderProduct o1, InventoryOrderProduct o2) {
		if (o1 == null || o2 == null)
		   return 0;
		
		String pi1 = o1.getProductBarcode().getProduct().getProductIdentity();
		String pi2 = o2.getProductBarcode().getProduct().getProductIdentity();
		
		if (StringUtils.isEmpty(pi1) || StringUtils.isEmpty(pi2))
			return pi2.compareTo(pi1);
		
		try {
			String yearQ1 = pi1.substring(0, 2);
			String yearQ2 = pi2.substring(0, 2);
			
			if (yearQ1.equals(yearQ2)) {
				pi1 = pi1.substring(2);
				pi2 = pi2.substring(2);
				
				String size1 = pi1.substring(1, 2);
				String size2 = pi2.substring(1, 2);
				
				if (size1.equals(size2)) {
				
					String gender1 = pi1.substring(0, 1);
					String gender2 = pi2.substring(0, 1);
					
					 if (gender1.equals(gender2)) {
						String sequence1 = pi1.substring(2);
						String sequence2 = pi2.substring(2);
							
							return sequence1.compareTo(sequence2);
						} else 
							return gender1.compareTo(gender2);
					} else 
						return size2.compareTo(size1);
			} else 
				return yearQ1.compareTo(yearQ2);
			
		} catch (Exception e) {
			return 0;
		}
		
	}

}
